package com.mi.soul.whale.module.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mi.rule.db.core.MiPage;
import com.mi.rule.db.core.MiWrapper;
import com.mi.rule.db.dao.MiDao;
import com.mi.rule.util.project.SRS;
import com.mi.soul.whale.module.system.service.SysUserService;
import com.mi.soul.whale.mybatis.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户表 服务实现类
 *
 * @author 王永吉
 */
@Slf4j
@Service
public class TemplateServiceImpl implements SysUserService {
    @Autowired
    private MiDao miDao;

    //添加
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SRS insert(SRS param) {
        SysUser user = param.parse(SysUser.class);
        //
        //检查数据重复
        //        long count = miDao.count(new MiWrapper<>(SysUser.class).eq(SysUser.ACCOUNT, user.getAccount()));
        //        if (count != 0) {
        //            return SRS.byError("账号已存在");
        //        }
        //
        miDao.insert(user);
        return SRS.bySuccess();
    }

    //删除
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SRS delete(SRS param) {
        JSONArray ids = param.getJSONArray("ids");
        miDao.deleteByIds(SysUser.class, ids);
        return SRS.bySuccess();
    }

    //修改
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SRS update(SRS param) {
        SysUser user = param.parse(SysUser.class);
        //
        //检查数据重复
        //        long count = miDao.count(new MiWrapper<>(SysUser.class)
        //                .ne("id", user.getId())
        //                .eq(SysUser.ACCOUNT, user.getAccount()));
        //        if (count != 0) {
        //            return SRS.byError("账号已存在");
        //        }
        //
        miDao.update(user);
        return SRS.bySuccess();
    }

    //分页
    @Override
    public SRS page(SRS param) {
        //条件
        String id = param.getString("id");
        String createTime = param.getString("createTime");
        MiWrapper<SysUser> miWrapper = new MiWrapper<>(SysUser.class);
        miWrapper.orderByAsc(SysUser.ID);
        //非空判断
        miWrapper.eq(StringUtils.isNoneBlank(id), SysUser.ID, id);
        miWrapper.eq(StringUtils.isNoneBlank(createTime), SysUser.CREATE_TIME, createTime);
        //查询结果
        IPage<SysUser> page = miDao.page(new MiPage<>(param), miWrapper);
        return SRS.bySuccess(page);
    }

    //列表
    @Override
    public SRS list(SRS param) {
        //条件
        String id = param.getString("id");
        MiWrapper<SysUser> miWrapper = new MiWrapper<>(SysUser.class);
        miWrapper.orderByAsc(SysUser.ID);
        miWrapper.eq(StringUtils.isNoneBlank(id), SysUser.ID, id);
        //结果
        List<SysUser> list = miDao.list(miWrapper);
        return SRS.bySuccess(list);
    }

    //详情
    @Override
    public SRS info(SRS param) {
        String id = param.getString("id");
        SysUser user = miDao.selectById(SysUser.class, id);
        return SRS.bySuccess(user);
    }
}